#include "stm32f10x.h"                  // Device header
#include "move.h"

uint8_t speed=70;
void CountSensor_Init(uint8_t speed)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_12|GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructureb;
	GPIO_InitStructureb.GPIO_Mode= GPIO_Mode_AF_PP;
	GPIO_InitStructureb.GPIO_Pin= GPIO_Pin_0 |GPIO_Pin_1;
	GPIO_InitStructureb.GPIO_Speed= GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructureb);
	
	TIM_InternalClockConfig(TIM2);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 100-1;//ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler = 720-1;//PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
	
	
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = speed;//CCR
	TIM_OC1Init(TIM2,&TIM_OCInitStructure);
	TIM_OC2Init(TIM2,&TIM_OCInitStructure);
	
	TIM_Cmd(TIM2, ENABLE);
}

uint8_t detect_l1()
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14);
}

uint8_t detect_r1()
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4);
}

uint8_t detect_straight()
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5);
}
uint8_t detect_l2()
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9);
}
uint8_t detect_r2()
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8);
}
uint8_t detect_r3()
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);
}
uint8_t detect_l3()
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12);
}
